import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

# 1.	完成数据集的加载
data = np.loadtxt('data2.txt', delimiter=',')
m = len(data)
x= data[:, :-1]
y = data[:, -1:]
# scale
mu = x.mean(axis=0)
sigma = x.std(axis=0)
x -= mu
x /= sigma

# 2.	完成数据切分
# 3.	切分训练集与测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.7, random_state=666)

# 4.	创建决策树模型，使用分类树
clf = DecisionTreeClassifier(max_depth=5)

# 5.	拟合数据
clf.fit(x_train, y_train)

# 6.	输出测试集预测值
print('测试集预测值')
h_test = clf.predict(x_test)
print(h_test)

# 7.	输出测试集准确率
s_test = clf.score(x_test, y_test)
print(s_test)
